// @file          GravicScanStation.groovy
// @author        Mac Radigan
// @description 

package ycoa.scan.process

import ycoa.xml.*
import org.w3c.dom.Element
import ycoa.configuration.YCOAConfiguration

import org.apache.log4j.Logger
import org.codehaus.groovy.runtime.TimeCategory

public class GravicScanStation extends ScanStation {

  static Logger log = Logger.getLogger(GravicScanStation.class)
  public ConfigObject config = YCOAConfiguration.getConfiguration()

  public boolean process(reference, directory, outfile, logs) { 
    log.debug("calling Gravic")
    try {
      def initialSize = 4096
      def outStream = new ByteArrayOutputStream(initialSize)
      def errStream = new ByteArrayOutputStream(initialSize)
      //def cmdSignin = """ "${config.tools.scan}" """
      def cmdSignin = "c:\\Program Files (x86)\\Gravic\\Remark Office OMR 7\\Roo70.exe "
      //cmdSignin += " /E /T $reference /I $directory /Oxml- $outfile "
      //cmdSignin += """ /H /Wh /E /T "$reference" /Not "$logs\\CP_[ClassReportID]\\Signin_CP_[ClassReportID]_[StudentID1].tif" /Ip "$directory" /Oxml $outfile """
      cmdSignin += """ /H /Wh /E /T "$reference" /Not "$logs" /Ip "$directory" /Oxml $outfile """
      log.info('  [execute]   '+cmdSignin)
      def startTime = new Date()
      def procSignin = cmdSignin.execute()
      procSignin.consumeProcessOutput(outStream, errStream)
      procSignin.waitForOrKill(4*60*1000)
      //procSignin.waitFor()
      def endTime = new Date()
      def elapsedTime = null
      use(TimeCategory) {
        elapsedTime = endTime.minus(startTime)
      }
      log.debug("Gravic processor elapsed time: ${elapsedTime}")
      if(procSignin.exitValue()) {
	log.info(errStream)
      } else {
	log.debug(outStream)
      }
      //new File('out.log') << outStream
      //new File('out.err') << errStream
      log.debug("done calling Gravic")
      return true
    } catch(e) {
      log.error("Error calling Gravic:  ${e.getMessage()}")
      throw new Exception("Error calling Gravic.", e)
    }
  } 

}

/* *EOF* */
